In this tutorial you learn how to set the state of your application using a JavaScript script. In the tutorial you create a fuel gauge, add a state manager which sets the gauge color, and use a JavaScript script to control the gauge state.
This video shows the result of the tutorial.
This tutorial assumes you understand the basics of working with Kanzi Studio. The best entry points for getting familiar with Kanzi Studio are:
The starting point of this tutorial is the Scripting states.kzproj Kanzi Studio project file stored in the <KanziWorkspace>/Tutorials/Scripting states/Assets directory.
The <KanziWorkspace>/Tutorials/Scripting states/Completed directory contains the completed Kanzi Studio project of this tutorial.
In this section you create the fuel level indicator which displays the amount of fuel on the fuel gauge. You create the brushes that you use for the colors on the fuel level indicator. In the next section you create a state manager that sets a brush for each state.
To create the fuel level indicator for the fuel gauge:
In this section you create a state manager with states that set how the fuel indicator looks when the level of fuel is full, half full, and empty. To indicate the amount of fuel, in each of the states you use one of the color brushes you created in the previous section.
To define the states for the fuel indicator:
You can set a state as the initial state by right-clicking the state and selecting Set as initial state in this group.
When State Tools are switched on Kanzi Studio keeps track of all property changes in your project. For this reason it is a good practice to switch the State Tools off when you are done setting the states in a specific state manager.
To add a property, right-click in the Properties, select Add Property, and then select the property you want to add.
For example, to add the Render Transformation property, right-click in the Properties, and select Add Property > Transform 2D > Render Transformation.
In this section you create a slider, which you use to simulate the fuel level. You bind the Layout Height property of the FuelLevel node to the value of the slider so that when you move the slider knob, you change the height of the fuel level indicator.
To create a slider to simulate the fuel level:
You can see in the Dictionaries window the list of resources in the resource dictionaries that the node selected in the Project can access.
{#Control/RangeConcept.Value}
With this binding you bind the Value property of the Control slider component to the Layout Height property of the fuel level indicator.
You can retrieve alias target nodes with bindings, the Kanzi Engine API, or scripting using the hash sign (#
) followed by the name of the alias, regardless of the node location in the project. For example, use #Control to access the Control slider node using its alias.
To add a property to a binding expression without typing, from the Properties drag the name of the property you want to add, and drop it to the Expression field in the Binding Argument Editor.
For example, to add the Value property of the Slider node, select the Slider node in the Project and drag the Value property from the Properties to the Expression field in the Binding Argument Editor.
In the Preview when you drag the slider knob, you change the value of the Layout Height property in the FuelLevel node. You set the height of the fuel level indicator based on the position of the slider knob.
In this section you create a JavaScript script to control the state of the fuel gauge. The script sets the application state to either Full, Half, or Empty every time you move the slider.
To create a JavaScript script to control the state of the fuel gauge:
Click Save.
You set Kanzi to execute the On Property Change trigger when the value of the Layout Height property in the FuelLevel node changes.
// Get the Layout Height property of the FuelLevel node. var height = node.getProperty("Node.Height"); // Set the state to Empty if the Layout Height is less than or equals 60 pixels. // Set the state to Half if the Layout Height is more than 60 but // less than or equals 300 pixels. // If the Layout Height is more than 300 pixels, set the state to Full. if (height <= 60) { node.goToState("StateGroup.Empty"); } else if (height > 60 && height <= 300) { node.goToState("StateGroup.Half"); } else { node.goToState("StateGroup.Full"); }
Kanzi executes the JavaScript script, which sets the application state to either Full, Half, or Empty every time you move the slider.
In the Preview when you drag the knob on the slider, the FuelLevel state changes based on the value of the Layout Height property of the FuelLevel node.
When the height of the FuelLevel node is:
In this tutorial you learned how to use a JavaScript script to control the state of an application. Now you can:
Tutorial: Use JavaScript to create a demo mode